package com.atxiao.controller;

import cn.hutool.core.util.XmlUtil;
import com.alibaba.fastjson.JSONObject;
import com.github.wxpay.sdk.WXPay;
import com.github.wxpay.sdk.WXPayConfigImpl;
import com.github.wxpay.sdk.WXPayDomainImpl;
import org.springframework.http.*;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.HashMap;

@RestController
public class SubAppidController {

    @RequestMapping("/bangdign")
    public void bangDing(@RequestBody JSONObject param){
        //返回值是XML,修改返回值为XML
        HashMap<String, Object> data = new HashMap<>();
        data.put("appid",param.get("APPID"));
        data.put("mch_id",param.get("MCH_ID"));
        data.put("sub_mch_id",param.get("SUB_MCH_ID"));
        data.put("sub_appid",param.get("SUB_APP_ID"));
        data.put("sign",param.get("E7DF9A69C68A7342D7B7D98AAD291B6A"));
        String res = XmlUtil.mapToXmlStr(data);
        String url="http://182.92.57.72:9090/secapi/mch/addsubdevconfig";
//        String url="https://101.91.0.140:443/secapi/mch/addsubdevconfig";

        RestTemplate restTemplate = new RestTemplate();
        MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
        mappingJackson2HttpMessageConverter.setSupportedMediaTypes(Arrays.asList(MediaType.APPLICATION_XML, MediaType.APPLICATION_PROBLEM_XML));
        restTemplate.getMessageConverters().add(mappingJackson2HttpMessageConverter);
        ResponseEntity<String> stringResponseEntity = restTemplate.postForEntity(url, res, String.class);
        HttpStatus statusCode = stringResponseEntity.getStatusCode();
        System.out.println("响应的结果："+statusCode);

    }


    @RequestMapping("/bangdign2")
    public void bangDing2(@RequestBody JSONObject param) throws Exception {
        //返回值是XML,修改返回值为XML
        HashMap<String, String> data = new HashMap<>();
        data.put("appid", (String) param.get("APPID"));
        data.put("mch_id",(String)param.get("MCH_ID"));
        data.put("sub_mch_id",(String)param.get("SUB_MCH_ID"));
        data.put("sub_appid",(String)param.get("SUB_APP_ID"));
        data.put("sign",(String)param.get("SIGN"));
        data.put("nonce_str","12345");
//        String res = XmlUtil.mapToXmlStr(data);
//        String url="http://182.92.57.72:9090/secapi/mch/addsubdevconfig";
//        String url="https://101.91.0.140:443/secapi/mch/addsubdevconfig";

        String mchID= (String) param.get("MCH_ID");
        //证书位置
        String certPath="C:\\Users\\19142\\Desktop\\xx\\apiclient_cert.p12";
        String urlSuffix="/secapi/mch/addsubdevconfig";
        //加载证书的流
        FileInputStream fileInputStream = new FileInputStream(new File(certPath));

        //域名的配置类
        WXPayDomainImpl wxPayDomain = new WXPayDomainImpl();
        //证书、密码配置类
        WXPayConfigImpl wxPayConfig = new WXPayConfigImpl(null, mchID, mchID, null, fileInputStream, wxPayDomain);
        WXPay wxPay = new WXPay(wxPayConfig);
        String resp = wxPay.requestWithCert(urlSuffix, data, 8000, 8000);
        System.out.println("==============================");
        System.out.println("resp:"+resp);
        System.out.println("==============================");


        //设置请求头
//        HttpHeaders headers = new HttpHeaders();
//        headers.setContentType(MediaType.APPLICATION_XML);


        //请求体
//        HttpEntity<String> formEntity = new HttpEntity<>(res, headers);
//        RestTemplate restTemplate = new RestTemplate();
//        restTemplate.postForEntity(url, res, String.class);

    }


}
